﻿// JScript 文件
//定义变量
//用户帐户
var ajaxUrl="/";
//登录地址
var loginUrl="http://www.vancl.com/";
//商品数
var itemCount=0;
$(document).ready(function()
{
    //设置用户信息
    setShoppingCar();
    //$("#spanCount").DMenu("#content");
    
    //放在site_nav中调用此函数,因为在帮助中心中,$("#welcome")是后生成的.
   //setLoginInfo();
});
function setLoginInfo()
{
	 if(hasLogin())
    {
        setWelcome();
    }
}
//判断用户是否登录
function hasLogin()
{
    return getCookie("UserLogin")!="";
}

//异步获取用户登录名称.
function setWelcome()
{
    $.getScript(ajaxUrl+"Usercenter/GetUserName.ashx", function()
            {
                if(typeof(data)!=undefined&&data!="")
                {
                    $("#login").html("<a class='top'  href='"+ajaxUrl+"login/UserLoginOut.aspx' target='_parent' >退出登录</a>");
                       $("#welcome").html("您好，<a class='top'  href='"+ajaxUrl+"UserCenter/OrderList.aspx' >"+data+"</a>。<a class='top'  href='"+ajaxUrl+"Login/UserLoginOut.aspx' target='_parent' >退出登录</a>");
                }
                else
                {
                    return;           
                }
            }
        );  
}

//获取购物车内的物品数
function setShoppingCar()
{
    var ShoppingCarInfocookie = getShoppingCookie("ShoppingCarInfo",false);
    if(ShoppingCarInfocookie!=null && ShoppingCarInfocookie != "undefined" && ShoppingCarInfocookie.length>0 && ShoppingCarInfocookie.indexOf("undefined")<0&& ShoppingCarInfocookie.indexOf("null")<0)
    {
        var cookies = ShoppingCarInfocookie.split("$");

        if(cookies.length>0)
        {
            $("#prdCount").val(cookies[1]);
            $("#price").html(cookies[0]);
        }else
        {
            $("#prdCount").val(0);
            $("#price").html(0);
        }
    }
    else
    {
	     $.getScript(ajaxUrl+"Usercenter/GetUserShoppingCarInfo.ashx", function()
                {
                
                    if(typeof car_info!='undefined'&&car_info!="")
                    {
                        $("#prdCount").val((car_info.TotCount>0?car_info.TotCount+GetPrizeCout() : car_info.TotCount));
                        $("#price").html(car_info.TotMoney+"");
                        var exp = new Date();
                        exp.setTime(exp.getTime() + 15*24*60*60*1000); 
                        setShoppingCookie("ShoppingCarInfo",$("#price").html()+"$"+$("#prdCount").val(),exp,false);
                    }
            } );
     }
  
}
//获取赠品的数量
function GetPrizeCout()
{
    var t = 0;
    var noLargessSelected = getCookie("noLargessSelected");
    var Rules_Product = getCookie("MustHasSKU");
    if(noLargessSelected!="undefined" && noLargessSelected!="")
    {
        var obj = noLargessSelected.split("@");
        for(var i=0;i<obj.length;i++)
        {
            var Largess = obj[i].split("$");
            if(Largess.length>1)
            {
                var Prouductobj = Largess[1].split(",");
                for(var j=0;j<Prouductobj.length;j++)
                {
                     var Product = Prouductobj[j].split("|");
                     t += parseInt(Product[1]);  
                }
            }
        }    
    }
    if(Rules_Product!="undefined" && Rules_Product!="")
    {
        var obj = Rules_Product.split("@");
        for(var i=0;i<obj.length;i++)
        {
            var Largess = obj[i].split("$");
            if(Largess.length>1)
            {
                var Prouductobj = Largess[1].split(",");
                for(var j=0;j<Prouductobj.length;j++)
                {
                     t += 1;  
                }
            }
        }    
    }
    return t;
}
function getShoppingCookie(c_name, doEsc) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length; value = document.cookie.substring(c_start, c_end);
            return doEsc ? unescape(value) : value;
        }
    }
    return "";
}
function setShoppingCookie(c_name, value, expires, doEsc) {
    document.cookie = c_name + "=" + (doEsc ? escape(value) : value) +
((expires == null) ? ";" : ";expires=" + expires + ";") + "path=/;domain=.vancl.com;";
}
